package com.ocom.company.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.master.EntityCompanykeyInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.RandomUtil;
import com.ocom.company.dao.IDaoCompanyKeyInfo;
import com.ocom.company.service.CompanyKeyInfoService;
import com.ocom.company.utils.sm2.SM2KeyHelper;
import com.ocom.company.utils.sm2.SM2KeyPair;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.bouncycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class CompanyKeyInfoServiceImpl extends ServiceImpl<IDaoCompanyKeyInfo, EntityCompanykeyInfo> implements CompanyKeyInfoService {

    //注入MngUserInfo 持久层
    @Autowired
    private IDaoCompanyKeyInfo daoCompanyKeyInfo;

    public static final String KEY_ALGORITHM = "RSA";

    private static final String PUBLIC_KEY = "PublicKey";

    private static final String PRIVATE_KEY = "PrivateKey";
    /**
     * 用于封装随机产生的公钥与私钥
     */
    private static Map<String, String> keyMap = new HashMap<>();
    /**
     * 密钥长度 于原文长度对应 以及越长速度越慢
     */
    private final static int KEY_SIZE = 2048;


    @Override
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    public EntityCompanykeyInfo getCompanyKeyInfo(Long companyId){
        QueryWrapper<EntityCompanykeyInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Transactional
    @Override
    public void AddCompanyKeyInfo(EntityCompanykeyInfo companykeyInfo) {
        //生成公钥和私钥
        try {
            SM2KeyPair sm2KeyPair = SM2KeyHelper.generateKeyPair();
//            ECPublicKeyParameters ecPublicKeyParameters = SM2KeyHelper.buildECPublicKeyParameters(sm2KeyPair);
//            ECPrivateKeyParameters ecPrivateKeyParameters = SM2KeyHelper.buildECPrivateKeyParameters(sm2KeyPair.getPrivateKey());
//
//            System.out.println("publicKeyX:"+ Hex.toHexString(sm2KeyPair.getPublicKeyX()));
//            System.out.println("publicKeyY:"+ Hex.toHexString(sm2KeyPair.getPublicKeyY()));
//            System.out.println("privateKey:"+ Hex.toHexString(sm2KeyPair.getPrivateKey()));
//            System.out.println("publicKey:" + Hex.toHexString(ecPublicKeyParameters.getQ().getEncoded(false)));
            //加密字符串
            String publicKey = Hex.toHexString(sm2KeyPair.getPublicKeyX()) + Hex.toHexString(sm2KeyPair.getPublicKeyY());
            String privateKey = Hex.toHexString(sm2KeyPair.getPrivateKey());

            if(privateKey.startsWith("00")){
                privateKey= privateKey.substring(2);
            }

            companykeyInfo.setPrivateKey(privateKey);
            companykeyInfo.setPublicKey(publicKey);

        }catch (Exception exception){
            exception.printStackTrace();
        }
        companykeyInfo.setCommKey(RandomUtil.getRandomString(36));
        daoCompanyKeyInfo.insert(companykeyInfo);
    }




    /*
     * @Description:2.18.查询单位秘钥信息(getCompanyKeyInfo)
     * @Author: lu 
     * @Date: 2022/4/26 11:28
     * @param comId: 
     * @param page: 
     * @param size:  
     * @return: java.util.List<com.ocom.common.entity.master.EntityCompanykeyInfo>
     **/
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityCompanykeyInfo> selectCompanyKeyList(Long comId) {
        QueryWrapper<EntityCompanykeyInfo> queryWrapper=new QueryWrapper<>();
        if (comId!=null && comId!=-1){
            queryWrapper.eq("com_id",comId);
        }
        queryWrapper.orderByDesc("com_id","create_time");
        return daoCompanyKeyInfo.selectList(queryWrapper);
    }

    /*
     * @Description:查询单位秘钥信息
     * @Author: lu 
     * @Date: 2022/4/26 13:23
     * @param comId: 
     * @param page: 
     * @param size:  
     * @return: java.util.List<com.ocom.common.entity.master.EntityCompanykeyInfo>
     **/
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public PageInfo<EntityCompanykeyInfo> selectCompanyKeyListPage(Long comId, Integer page, Integer size) {
        QueryWrapper<EntityCompanykeyInfo> queryWrapper=new QueryWrapper<>();
        if (comId!=null && comId!=-1){
            queryWrapper.eq("com_id",comId);
        }
        queryWrapper.orderByDesc("com_id","create_time");
        PageHelper.startPage(page,size);
        List<EntityCompanykeyInfo> pointInfoPageInfo=this.list(queryWrapper);
        PageInfo<EntityCompanykeyInfo> pageInfo=new PageInfo<>(pointInfoPageInfo);
        return pageInfo;
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public int updateCompanyCommKey(Long comId, String commKey) {
        EntityCompanykeyInfo entityCompanykeyInfo=new EntityCompanykeyInfo();
        entityCompanykeyInfo.setComId(comId);
        entityCompanykeyInfo.setCommKey(RandomUtil.getRandomString(36));
        //生成公钥和私钥
        try {
            SM2KeyPair sm2KeyPair = SM2KeyHelper.generateKeyPair();
//            ECPublicKeyParameters ecPublicKeyParameters = SM2KeyHelper.buildECPublicKeyParameters(sm2KeyPair);
//            ECPrivateKeyParameters ecPrivateKeyParameters = SM2KeyHelper.buildECPrivateKeyParameters(sm2KeyPair.getPrivateKey());
//
//            System.out.println("publicKeyX:"+ Hex.toHexString(sm2KeyPair.getPublicKeyX()));
//            System.out.println("publicKeyY:"+ Hex.toHexString(sm2KeyPair.getPublicKeyY()));
//            System.out.println("privateKey:"+ Hex.toHexString(sm2KeyPair.getPrivateKey()));
//            System.out.println("publicKey:" + Hex.toHexString(ecPublicKeyParameters.getQ().getEncoded(false)));
            //加密字符串
            String publicKey = Hex.toHexString(sm2KeyPair.getPublicKeyX()) + Hex.toHexString(sm2KeyPair.getPublicKeyY());
            String privateKey = Hex.toHexString(sm2KeyPair.getPrivateKey());

            if(privateKey.startsWith("00")){
                privateKey= privateKey.substring(2);
            }

            entityCompanykeyInfo.setPrivateKey(privateKey);
            entityCompanykeyInfo.setPublicKey(publicKey);

        }catch (Exception exception){
            exception.printStackTrace();
        }
        return   daoCompanyKeyInfo.insert(entityCompanykeyInfo);
    }



}
